/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.utl.ist.isr.livetrack.ui;

import java.awt.event.ActionEvent;
import javax.swing.Action;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.PropertySupport.Reflection;
import org.openide.nodes.Sheet;
import org.openide.util.HelpCtx;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;
import org.openide.util.lookup.Lookups;
import pt.utl.ist.isr.livetrack.server.LTServer;
import pt.utl.ist.isr.livetrack.ui.Settings.Server;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class DisconnectedServerNode extends AbstractNode {

    private static Action[] actions = new Action[]{
        new org.openide.util.actions.SystemAction() {

            @Override
            public String getName() {
                return NbBundle.getMessage(this.getClass(), "DeleteServer");
            }

            @Override
            public HelpCtx getHelpCtx() {
                return null;
            }

            @Override
            public void actionPerformed(ActionEvent ae) {
                Settings.loadSettings().getServers().remove(Utilities.actionsGlobalContext().lookup(DisconnectedServerNode.class).server);
                Settings.loadSettings().save();
                ServersNodeChildFactory.getInstance().refresh();
            }
        }};
    private Sheet sheet;
    private Sheet.Set set;
    private Server server;

    public DisconnectedServerNode(Server server) {
        super(Children.LEAF, Lookups.singleton(server));
        this.server = server;
        setName(server.toString() + NbBundle.getMessage(this.getClass(), "Disconnected"));
        init();
    }

    private void init() {

        sheet = Sheet.createDefault();
        set = Sheet.createPropertiesSet();
        final LTServer obj = getLookup().lookup(LTServer.class);

//        try {
//            addProperties(new PropertySupport.Reflection[]{},
//                    new String[]{});
//
//
//        } catch (NoSuchMethodException ex) {
//            ErrorManager.getDefault();
//        }

        sheet.put(set);
    }

    private void addProperties(Reflection[] props, String[] names) {
        for (int i = 0; i < props.length; i++) {
            Property property = props[i];
            property.setName(names[i]);
            set.put(property);
        }
    }

    @Override
    protected Sheet createSheet() {
        return sheet;
    }

    @Override
    public Action[] getActions(boolean context) {
        return actions;
    }
}
